package com.fuss.activitytest

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import android.util.Log
import android.view.Menu
import android.view.MenuItem
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import com.fuss.activitytest.launchmode.LauncherModeActivity
import kotlinx.android.synthetic.main.activity_main.*

class FirstActivity : AppCompatActivity() {

    companion object {

        @JvmStatic
        val TAG: String = FirstActivity::class.java.simpleName
        @JvmStatic
        val REQUEST_CODE_TO_FOURACTIVITY: Int = 1
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        initListener()
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {

        menuInflater.inflate(R.menu.first, menu)
        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {

        when (item.itemId) {
            R.id.add_item -> onShowToastMessage("You clicked Add!")
            R.id.remove_item -> {
                onShowToastMessage("You clicked Remove!")
            }
            else -> onShowToastMessage("UnKnown")
        }
        return true
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)

        when (requestCode) {
            REQUEST_CODE_TO_FOURACTIVITY -> if (resultCode == Activity.RESULT_OK) {
                val returnMessage = data?.getStringExtra("data_return_message")
                Log.w(TAG, "接收到从 FourActivity 界面返回的数据：$returnMessage")
            }
        }
    }

    override fun onDestroy() {
        super.onDestroy()
        Log.e(TAG, "${this} ->onDestroy")
    }

    private fun initListener() {

        button1.setOnClickListener {

            onShowToastMessage("You clicked Finish!")
            finish()
        }

        to_second_activity_btn.setOnClickListener {

            onShowToastMessage("To SecondActivity!")
            val intent = Intent(this, SecondActivity::class.java)
            startActivity(intent)
        }

        to_second_activity_btn2.setOnClickListener {

            onShowToastMessage("To SecondActivity!")
            val intent = Intent("com.fuss.activitytest.MY_ACTION_START_SECOND_ACTIVITY")
            intent.addCategory("com.fuss.activitytest.MY_CATEGORY_SECOND_ACTIVITY")
            startActivity(intent)
        }

        to_third_activity_btn.setOnClickListener {

            onShowToastMessage("To ThirdActivity!")
            val intent = Intent("com.fuss.activitytest.MY_ACTION_START_THIRD_ACTIVITY")
            startActivity(intent)
        }

        to_baidu_btn.setOnClickListener {

            val intent = Intent(Intent.ACTION_VIEW)
            intent.data = Uri.parse("https://www.baidu.com")
            startActivity(intent)
        }

        to_four_btn.setOnClickListener {

            val intent = Intent(this, FourActivity::class.java)
            intent.putExtra("extra_data", "https://cn.bing.com")
            startActivityForResult(intent, REQUEST_CODE_TO_FOURACTIVITY)
        }

        launch_mode_btn.setOnClickListener {

            // 方法一：常规操作
//            val intent = Intent(this, LauncherModeActivity::class.java)
//            startActivity(intent)
            // 方法二：调用 LauncherModeActivity 的 navigateTo(Context) 方法
            LauncherModeActivity.navigateTo(this)
        }
    }

    /**
     * 弹出吐司消息，并解决消息带有应用名的问题
     */
    private fun onShowToastMessage(message: String) {

        val toast: Toast = Toast.makeText(this, "", Toast.LENGTH_SHORT)
        toast.setText(message)
        toast.show()
    }
}
